package com.ctv.my.customview.BaiajCustomView.View;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ctv.my.customview.BaiajCustomView.utils.UIUtil;
import com.ctv.my.customview.R;

/**
 * Created by baiaj on 2017/7/28.
 */

public class SmileView extends LinearLayout implements Animator.AnimatorListener {

    private static final String WHITE = "#ffffff";
    private static final String PRECENT = "%";
    private static final String LIKE_STR = "喜欢";
    private static final String DISLIKE_STR = "无感";
    private static final int DIVIDER_WIDTH = 3;
    private static final int DIVIDER_HEIGHT = 30;
    private static final int DIVIDER_MARGIN = 16;

    private LinearLayout llytLikeAll, llytDisAll, llytIvLikeAll, llytIvDisAll;
    private TextView tvLikePrecent, tvLikeStr, tvDisPrecent, tvDisStr;
    private ImageView ivLike, ivDisLike;
    private View divider;

    private int ivLikeRadius = 20;
    private int ivDisLikeRadius = 20;
    private int tvPrecentSp = 20;
    private int tvStrSp = 16;
    private int ivMargin;

    private AnimationDrawable animationLike;
    private AnimationDrawable animationDisLike;
    private ValueAnimator animationExtend;

    private int likePrecent;
    private int dislikePrecent;

    private String likeStr = LIKE_STR;
    private String disLikeStr = DISLIKE_STR;

    private animType mType;
    private likeType mSmileType;

    public SmileView(Context context) {
        this(context, null);
    }

    public SmileView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SmileView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        ivMargin = UIUtil.dipToPx(getContext(), 8);
        init();
        initEvent();
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.SmileView);
        ivLikeRadius = ta.getInteger(R.styleable.SmileView_radius,20);
        likeStr = ta.getString(R.styleable.SmileView_likestr);
        disLikeStr = ta.getString(R.styleable.SmileView_dislikestr);
        ta.recycle();
    }

    private void init() {
        // 初始化like iv
        ivLike = new ImageView(getContext());
        ivLike.setBackgroundResource(R.drawable.animation_like);
        animationLike = (AnimationDrawable) ivLike.getBackground();
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(UIUtil.dipToPx(getContext(), ivLikeRadius * 2), UIUtil.dipToPx(getContext(), ivLikeRadius * 2));
        params1.setMargins(ivMargin, ivMargin, ivMargin, ivMargin);
        ivLike.setLayoutParams(params1);
        // 初始化包裹like iv的Linearlayout
        llytIvLikeAll = new LinearLayout(getContext());
        llytIvLikeAll.setBackgroundResource(R.drawable.likeact_smile_llyt_white_bg);
        // 初始化like tv precent
        tvLikePrecent = new TextView(getContext());
        tvLikePrecent.setGravity(Gravity.CENTER);
        tvLikePrecent.setTextColor(Color.parseColor(WHITE));
        tvLikePrecent.setTextSize(tvPrecentSp);
        tvLikePrecent.setVisibility(View.GONE);
        // 初始化like tv str
        tvLikeStr = new TextView(getContext());
        tvLikeStr.setGravity(Gravity.CENTER);
        tvLikeStr.setTextColor(Color.parseColor(WHITE));
        tvLikeStr.setTextSize(tvStrSp);
        tvLikeStr.setText(likeStr);
        tvLikeStr.setVisibility(View.GONE);
        // 初始化包裹like 的 view
        llytLikeAll = new LinearLayout(getContext());
        llytLikeAll.setOrientation(VERTICAL);
        // add
        llytIvLikeAll.addView(ivLike);
        llytLikeAll.addView(tvLikePrecent);
        llytLikeAll.addView(tvLikeStr);
        llytLikeAll.addView(llytIvLikeAll);
        // 初始化dislike iv
        ivDisLike = new ImageView(getContext());
        ivDisLike.setBackgroundResource(R.drawable.animation_dislike);
        animationDisLike = (AnimationDrawable) ivDisLike.getBackground();
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(UIUtil.dipToPx(getContext(), ivLikeRadius * 2), UIUtil.dipToPx(getContext(), ivLikeRadius * 2));
        params2.setMargins(ivMargin, ivMargin, ivMargin, ivMargin);
        ivDisLike.setLayoutParams(params2);
        // 初始化包裹dislike iv的Linearlayout
        llytIvDisAll = new LinearLayout(getContext());
        llytIvDisAll.setBackgroundResource(R.drawable.likeact_smile_llyt_white_bg);
        // 初始化dislike tv precent
        tvDisPrecent = new TextView(getContext());
        tvDisPrecent.setGravity(Gravity.CENTER);
        tvDisPrecent.setTextColor(Color.parseColor(WHITE));
        tvDisPrecent.setTextSize(tvPrecentSp);
        tvDisPrecent.setVisibility(View.GONE);
        // 初始化dislike tv str
        tvDisStr = new TextView(getContext());
        tvDisStr.setGravity(Gravity.CENTER);
        tvDisStr.setTextColor(Color.parseColor(WHITE));
        tvDisStr.setTextSize(tvStrSp);
        tvDisStr.setText(disLikeStr);
        tvDisStr.setVisibility(View.GONE);
        // 初始化包裹dislike 的 view
        llytDisAll = new LinearLayout(getContext());
        llytDisAll.setOrientation(VERTICAL);
        // add
        llytIvDisAll.addView(ivDisLike);
        llytDisAll.addView(tvDisPrecent);
        llytDisAll.addView(tvDisStr);
        llytDisAll.addView(llytIvDisAll);
        // 初始化divider
        divider = new View(getContext());
        divider.setBackgroundResource(R.color.color_divider_bg);
        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(UIUtil.dipToPx(getContext(), DIVIDER_WIDTH), UIUtil.dipToPx(getContext(), DIVIDER_HEIGHT));
        params3.setMargins(UIUtil.dipToPx(getContext(), DIVIDER_MARGIN), 0, UIUtil.dipToPx(getContext(), DIVIDER_MARGIN), UIUtil.dipToPx(getContext(), DIVIDER_MARGIN));
        divider.setLayoutParams(params3);
        // 总布局
        setOrientation(HORIZONTAL);
        setGravity(Gravity.BOTTOM);
        addView(llytDisAll);
        addView(divider);
        addView(llytLikeAll);
    }

    private void initEvent() {
        ivLike.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mType = animType.EXTEND;
                mSmileType = likeType.LIKE;
                ivLike.setOnClickListener(null);
                ivDisLike.setOnClickListener(null);
                setTvVisible(View.VISIBLE);
                // 更换背景
                llytIvLikeAll.setBackgroundResource(R.drawable.likeact_smile_llyt_yellow_bg);
                llytIvDisAll.setBackgroundResource(R.drawable.likeact_smile_llyt_white_bg);
                // 重置帧动画
                ivDisLike.setBackground(null);
                ivDisLike.setBackgroundResource(R.drawable.animation_dislike);
                animationDisLike = (AnimationDrawable) ivDisLike.getBackground();
                extendAnim();
            }
        });
        ivDisLike.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mType = animType.EXTEND;
                mSmileType = likeType.DISLIKE;
                ivLike.setOnClickListener(null);
                ivDisLike.setOnClickListener(null);
                setTvVisible(View.VISIBLE);
                // 更换背景
                llytIvDisAll.setBackgroundResource(R.drawable.likeact_smile_llyt_yellow_bg);
                llytIvLikeAll.setBackgroundResource(R.drawable.likeact_smile_llyt_white_bg);
                // 重置帧动画
                ivLike.setBackground(null);
                ivLike.setBackgroundResource(R.drawable.animation_like);
                animationLike = (AnimationDrawable) ivLike.getBackground();
                extendAnim();
            }
        });
    }


    // region 开放的接口

    /**
     * 设置百分比
     *
     * @param dislike 不喜欢的百分比
     * @param like    喜欢的百分比
     */
    public void setPrecent(int dislike, int like) throws Exception {
        if (dislike + like == 100) {
            dislikePrecent = dislike;
            likePrecent = like;
            tvLikePrecent.setText(likePrecent + PRECENT);
            tvDisPrecent.setText(dislikePrecent + PRECENT);
        } else {
            ivLike.setOnClickListener(null);
            ivDisLike.setOnClickListener(null);
            throw new Exception("传入数值不对");
        }
    }

    public void setDivider(int width, int height, int leftMargin, int topMargin, int rightMargin, int bottomMargin) {
        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(UIUtil.dipToPx(getContext(), width), UIUtil.dipToPx(getContext(), height));
        params3.setMargins(UIUtil.dipToPx(getContext(), leftMargin), UIUtil.dipToPx(getContext(), topMargin),
                UIUtil.dipToPx(getContext(), rightMargin), UIUtil.dipToPx(getContext(), bottomMargin));
        divider.setLayoutParams(params3);
    }

    // endregion

    // region 私有方法

    private void setTvVisible(int visible) {
        tvLikePrecent.setVisibility(visible);
        tvLikeStr.setVisibility(visible);
        tvDisPrecent.setVisibility(visible);
        tvDisStr.setVisibility(visible);
    }

    // 设置伸展动画
    private void extendAnim() {
        final int max = Math.max(likePrecent * 3, dislikePrecent * 3);
        animationExtend = ValueAnimator.ofInt(5, max);
        animationExtend.setDuration(500);
        animationExtend.addListener(this);
        animationExtend.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                if (value <= likePrecent * 3) {
                    setIvParams(ivLike, value);
                }
                if (value <= dislikePrecent * 3) {
                    setIvParams(ivDisLike, value);
                }
            }
        });
        animationExtend.start();
    }

    // 设置收缩动画
    private void backAnim() {
        final int max = Math.max(likePrecent * 3, dislikePrecent * 3);
        animationExtend = ValueAnimator.ofInt(max, 0);
        animationExtend.setDuration(500);
        animationExtend.addListener(this);
        animationExtend.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                if (value <= likePrecent * 3) {
                    setIvParams(ivLike, value);
                }
                if (value <= dislikePrecent * 3) {
                    setIvParams(ivDisLike, value);
                }
            }
        });
        animationExtend.start();
    }

    // 设置iv的marginbottom
    private void setIvParams(ImageView iv, int margin) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(UIUtil.dipToPx(getContext(), ivLikeRadius * 2),
                UIUtil.dipToPx(getContext(), ivLikeRadius * 2));
        params.setMargins(ivMargin, ivMargin, ivMargin, ivMargin + UIUtil.dipToPx(getContext(), margin));
        iv.setLayoutParams(params);
    }

    // 设置上下偏移动画
    private void transLationYAnim(View view) {
        ObjectAnimator animatior = ObjectAnimator.ofFloat(view, "translationY", -10.0f, 0.0f, 10.0f, 0.0f, -10.0f, 0.0f, 10.0f, 0);
        animatior.setRepeatMode(ObjectAnimator.RESTART);
        animatior.setDuration(1500);
        animatior.start();
        animatior.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animator) {
                animationLike.stop();
                mType = animType.BACK;
                backAnim();
            }
        });
    }

    //    // 设置左右偏移动画
    private void transLationXAnim(View view) {
        ObjectAnimator animatior = ObjectAnimator.ofFloat(view, "translationX", -10.0f, 0.0f, 10.0f, 0.0f, -10.0f, 0.0f, 10.0f, 0);
        animatior.setRepeatMode(ObjectAnimator.RESTART);
        animatior.setDuration(1500);
        animatior.start();
        animatior.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animator) {
                animationDisLike.stop();
                mType = animType.BACK;
                backAnim();
            }
        });
    }

    // endregion

    // region 动画回调

    @Override
    public void onAnimationStart(Animator animator) {

    }

    @Override
    public void onAnimationEnd(Animator animator) {
        if (mType == animType.EXTEND) {
            // 伸展结束
            // 开始
            if (mSmileType == likeType.LIKE) {
                animationLike.start();
                transLationYAnim(ivLike);
            } else {
                animationDisLike.start();
                transLationXAnim(ivDisLike);
            }
        } else {
            // 收缩结束
            initEvent();
            setTvVisible(View.GONE);
        }
    }

    @Override
    public void onAnimationCancel(Animator animator) {

    }

    @Override
    public void onAnimationRepeat(Animator animator) {

    }

    // endregion

    private enum animType {
        EXTEND, BACK
    }

    private enum likeType {
        LIKE, DISLIKE
    }
}
